package org.jeecg.modules.sggg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.sggg.entity.SgDrugRecord;
import org.jeecg.modules.sggg.mapper.SgDrugRecordMapper;
import org.jeecg.modules.sggg.service.ISgDrugRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 药品库
 * @Author: jeecg-boot
 * @Date: 2022-10-26
 * @Version: V1.0
 */
@Service
public class SgDrugRecordServiceImpl extends ServiceImpl<SgDrugRecordMapper, SgDrugRecord> implements ISgDrugRecordService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SgDrugRecordMapper drugRecordMapper;


    @Async
    public void riderSync() {
        redisUtil.del("template_drug");
        List<Map<String, Object>> list = new ArrayList<>();
        drugRecordMapper.selectList(new QueryWrapper<>()).forEach(drug -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", drug.getName());
            Map<String, Object> valueMap = new HashMap<>();
            valueMap.put("name", drug.getName());
            valueMap.put("dose", drug.getDose());
            valueMap.put("directions", drug.getDirections());
            valueMap.put("avoid", drug.getAvoid());
            valueMap.put("remark", drug.getRemark());
            map.put("value", valueMap);
            list.add(map);
        });
        redisUtil.set("template_drug", list);
    }

    public Object queryRedis() {
        return redisUtil.get("template_drug");
    }

}
